package com.qf.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.config.AlaryReadListener;
import com.qf.entity.Alary;
import com.qf.entity.TaxReturn;
import com.qf.service.AlaryService;
import com.qf.service.TaxReturnService;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/money/money1")
@CrossOrigin
public class AlaryController {

    @Autowired
    private AlaryService service;

    /*查询全部*/
    @GetMapping("/list")
    public R findAll(@RequestParam HashMap<String,String> map){
        int pageNum = Integer.parseInt(map.get("pageNum"));
        int pageSize = Integer.parseInt(map.get("pageSize"));
        PageHelper.startPage(pageNum,pageSize);

        List<Alary> list = service.findAll(map);
        PageInfo<Alary> info = new PageInfo<>(list);
        return R.ok(info);
    }

    /*通过id删除*/
    @GetMapping("/del")
    public R deleteById(int id) {
        boolean isok = service.deleteById(id);
        System.out.println(isok);

        return R.ok(isok);
    }

    /*批量删除*/
    @GetMapping("/delBatch")
    public R delBatch(@RequestParam List<Integer> ids){
        boolean isok = service.delBatch(ids);
        return R.ok(isok);
    }

    /*添加*/
    @PostMapping("/add")
    public R addAlary(@RequestBody Alary alary) {
        boolean isok = service.addAlary(alary);
        return R.ok(isok);
    }

    /*更正*/
    @PostMapping("/change")
    public R change(@RequestBody Alary alary) {
        boolean isok = service.change(alary);
        return R.ok(isok);
    }

    /*上传Excel文件*/
    @PostMapping("/upload/excel")
    public R uploadExcel(MultipartFile file)  {
        try{
            EasyExcel.read(file.getInputStream(),
                            Alary.class,
                            new AlaryReadListener(service))
                    .sheet().doRead();
        } catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok();
    }

    /*导出Excel*/
    @GetMapping("/download/excel")
    public void downloadExcel(HttpServletResponse response) throws IOException {
        // 查询数据库全部数据
        List<Alary> list = service.findAll(null);

        //导出.xls用这个
        //response.setContentType("application/vnd.ms-excel");

        //导出.xlsx用这个
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        //这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("工薪表","UTF-8");
        response.setHeader("Content-disposition","attachment;filename=" + fileName + ".xlsx");

        //easyexcel导出工具
        EasyExcel.write(response.getOutputStream(),
                Alary.class).autoCloseStream(Boolean.FALSE)
                .sheet("工薪表").doWrite(list);
    }

}
